package com.yan_jiu_sheng.LeetCodeHot100;

/**
 * https://leetcode.cn/problems/edit-distance/?envType=study-plan-v2&envId=top-100-liked
 * 动态规划 编辑距离 通过
 * 参考视频理解：https://www.bilibili.com/video/BV1sA411B73r/?spm_id_from=333.337.search-card.all.click&vd_source=fc4b1ad9e6d85555b8581640913d36a5
 *
 * @author yulongtian
 * @create 2024-07-28 14:59
 */
public class Test93 {
    public static void main(String[] args) {
//        System.out.println(new Test93().minDistance("intention", "execution"));
        System.out.println(new Test93().minDistance("a", "bc"));
    }

    public int minDistance(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();
        int[][] dp = new int[m + 1][n + 1];
        dp[0][0] = 0;
        for (int i = 1; i < m + 1; i++) {
            dp[i][0] = i;
        }
        for (int j = 1; j < n + 1; j++) {
            dp[0][j] = j;
        }
//        word1 = "#" + word1;
//        word2 = "#" + word2;
        for (int i = 1; i < m + 1; i++) {
            for (int j = 1; j < n + 1; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i][j - 1], dp[i - 1][j]));
                }
            }
        }

        return dp[m][n];
    }

}
